<?php
include_once('./inc/config.php');
if(is_logged_in()) redirect('dashboard.php');	

$mode = $_POST['mode'];
$actions = array();
switch($mode)
{

	case 'check':
		$actions = check_form();
	break;	

	default:
 	break;
	
}

/** 
 * Check Form
 *
 * @descp Validate User in the DB
 * @access public
 * @param  NA	 
 * @return void
 */
function check_form()
{
	global $dbobj, $proj_config;
	
	$ses_arr = array('user_id','user_email');
	if(!session_register($ses_arr))
		session_register($ses_arr);

	$set = array();
	
	$errors = '';
	
	$user_name 	= $_POST['user_name'];
	$user_pwd 	= $_POST['user_pwd'];
    $d_name 	= $_POST['d_name'];
	$user_co_name 	= $_POST['user_co_name'];
	$user_co_phn 	= $_POST['user_co_phn'];
	$user_co_add 	= $_POST['user_co_add'];
	
	if(empty($user_name) || strlen($user_name) < 5)
	{
	    $set['errors'] .= '<li>&raquo; Please enter a valid username (min 5 letters)</li>';
	}
    if(empty($user_pwd) || strlen($user_pwd) < 5)
	{
            $set['errors'] .= '<li>&raquo; Please enter a valid password (min 5 letters and mix of letters &amp; numbers)</li>';
	}
	if(empty($user_co_name) || strlen($user_co_name) < 2)
	{
            $set['errors'] .= '<li>&raquo; Please enter a valid company name (min 2 letters)</li>';
	}
	if(empty($user_co_phn) || strlen($user_co_phn) < 5)
	{
            $set['errors'] .= '<li>&raquo; Please enter a valid company phone no (min 5 letters)</li>';
	}
	if(empty($user_co_add) || strlen($user_co_add) < 5)
	{
            $set['errors'] .= '<li>&raquo; Please enter a valid company address</li>';
	}
    if(empty($d_name) || strlen($d_name) < 5)
	{
            $set['errors'] .= '<li>&raquo; Please enter a valid domain name (ex: www.abc.com)</li>';
	}
    else
	{
		if (!preg_match ($proj_config['domain_val'] , $d_name)) 
		{
			$set['errors'] .= '<li>&raquo; Please enter a valid domain name (ex: www.abc.com)</li>';
		}
	}
	    
    if(empty($set['errors']))        
	{
		$query = "SELECT user_email FROM ".TBL_USERS." WHERE user_email = '".$dbobj->MySQLDB_Sanitize($user_name)."'";			
		// Perform Query
		$result = $dbobj->MySQLDB_GetData($query);		
		// Gwet num rows from the result set	
		$no_rows = $dbobj->MySQLDB_Numrows();					
		if($no_rows > 0)
		{		
			$set['errors'] .= '<li>&raquo; Username already exists in the system. Please login to view details</li>';	
		}		
		$dbobj->MySQLDB_Free($result);
	
		$query = "SELECT d_name  FROM ".TBL_DOMAINS." WHERE d_name = '".$dbobj->MySQLDB_Sanitize($d_name)."'";			
		// Perform Query
		$result = $dbobj->MySQLDB_GetData($query);		
		// Gwet num rows from the result set	
		$no_rows = $dbobj->MySQLDB_Numrows();				
		if($no_rows > 0)
		{		
			$set['errors'] .= '<li>&raquo; Domain name already exists in the system. Please login to view details</li>';	
		}		
		$dbobj->MySQLDB_Free($result);
		
		if(empty($set['errors']))        
		{
			$query = "INSERT INTO ".TBL_USERS." 
						(`user_id`, `user_email`, `user_pwd`, 
						`user_co_name`, `user_co_phn`, `user_co_add`,
						`user_status`, `user_created`)
						VALUES 
						(NULL, 
						'".$dbobj->MySQLDB_Sanitize(trim($user_name))."', 
						MD5('".trim($user_pwd)."'),
						'".$dbobj->MySQLDB_Sanitize(trim($user_co_name))."',
						'".$dbobj->MySQLDB_Sanitize(trim($user_co_phn))."',
						'".$dbobj->MySQLDB_Sanitize(trim($user_co_add))."',
						'1', 
						'".date('Y-m-d H:i:s')."'
						)";			
			// Perform Query
			$user_id = $dbobj->MySQLDB_Insert($query);
			
			$web_code = add_webcode($d_name,$user_id);
			
			$set['congmsg'] = '<li>You have successfully completed your registration. Please login to view details</li>';
			$set['web_code'] = $web_code;
			$set['d_name'] = $d_name;
		}
	}
	return $set;  
}


/*************************************************************/
//Do not chnage this part
include(BASEPATH . 'views/header.php');//include header file
include(BASEPATH . 'views/register.php');//include body part
include(BASEPATH . 'views/footer.php');//include footer file
/************************************************************/
?>